package sxkyxt.kjyxgs.cn.ishop_gather_uploading_android;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * @version V1.0 <描述当前版本功能>.
 * @filename: sxkyxt.kjyxgs.cn.ishop_gather_uploading_android
 * @Author HY  on {Administrator}.
 * @Org 山西科远信通科技有限公司.
 * @Description: ${TODO} .
 * @Motto 大梦谁先觉, 贫僧我自知..
 * @Date 2018/11/28
 */
public class PhotoActivity extends AppCompatActivity {

    private static final String TAG = "PhotoActivity";
    private ListView mActivityPhotoShowDataList;
    private PhotoListAdapter photoListAdapter;

    private MainBean mainBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        initView();
        initContent();
    }

    private void initView() {
        mActivityPhotoShowDataList = (ListView) findViewById(R.id.list_activity_photo_show_data);
        mainBean = getIntent().getParcelableExtra("photoData");
    }

    private void initContent() {
        photoListAdapter = new PhotoListAdapter(this, mainBean.getExtend().getPageinfo().getList());
        mActivityPhotoShowDataList.setAdapter(photoListAdapter);
    }


    class PhotoListAdapter extends BaseAdapter implements View.OnClickListener {

        private Context context;
        private List<MainBean.ExtendBean.PageinfoBean.ListBean> list;


        public PhotoListAdapter(Context context, List<MainBean.ExtendBean.PageinfoBean.ListBean> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PhotoHolder photoHolder;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.adapter_list_show_photo_data, null);
                photoHolder = new PhotoHolder();
                this.initView(convertView, photoHolder);
                convertView.setTag(photoHolder);
            } else {
                photoHolder = (PhotoHolder) convertView.getTag();
            }

            //TODO add adapter data loading
            Picasso.with(context)
                    .load(CONSTS.HOST + list.get(position).getGpic())
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher_round)
                    .into(photoHolder.mAdapterListShowPhotoDataShowProductPhotoImage);
            Picasso.with(context)
                    .load(CONSTS.HOST  + list.get(position).getGdetailpic())
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher_round)
                    .into(photoHolder.mAdapterListShowPhotoDataShowProductBarcodeImage);
            photoHolder.mAdapterListShowPhotoDataShowProductNameText.setText(list.get(position).getGname());
            photoHolder.mAdapterListShowPhotoDataShowProductTypeText.setText(list.get(position).getGintroduction());
            photoHolder.mAdapterListShowPhotoDataShowProductSizeText.setText(list.get(position).getGstandard());
            photoHolder.mAdapterListShowPhotoDataShowProductVarietiesText.setText(list.get(position).getGtname());

            return convertView;
        }

        private void initView(@NonNull final View itemView, PhotoHolder photoHolder) {
            photoHolder.mAdapterListShowPhotoDataShowProductPhotoImage = (ImageView) itemView.findViewById(R.id.image_adapter_list_show_photo_data_show_product_photo);
            photoHolder.mAdapterListShowPhotoDataShowProductNameText = (TextView) itemView.findViewById(R.id.text_adapter_list_show_photo_data_show_product_name);
            photoHolder.mAdapterListShowPhotoDataShowProductTypeText = (TextView) itemView.findViewById(R.id.text_adapter_list_show_photo_data_show_product_type);
            photoHolder.mAdapterListShowPhotoDataShowProductSizeText = (TextView) itemView.findViewById(R.id.text_adapter_list_show_photo_data_show_product_size);
            photoHolder.mAdapterListShowPhotoDataShowProductVarietiesText = (TextView) itemView.findViewById(R.id.text_adapter_list_show_photo_data_show_product_varieties);
            photoHolder.mAdapterListShowPhotoDataShowProductBarcodeImage = (ImageView) itemView.findViewById(R.id.image_adapter_list_show_photo_data_show_product_barcode);
            photoHolder.mAdapterListShowPhotoDataClickProductDelText = (TextView) itemView.findViewById(R.id.text_adapter_list_show_photo_data_click_product_del);
            photoHolder.mAdapterListShowPhotoDataClickProductDelText.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.text_adapter_list_show_photo_data_click_product_del:
                    // TODO 18/11/28
                    break;
                default:
                    break;
            }
        }

        class PhotoHolder {
            private ImageView mAdapterListShowPhotoDataShowProductPhotoImage;
            private TextView mAdapterListShowPhotoDataShowProductNameText;
            private TextView mAdapterListShowPhotoDataShowProductTypeText;
            private TextView mAdapterListShowPhotoDataShowProductSizeText;
            private TextView mAdapterListShowPhotoDataShowProductVarietiesText;
            private ImageView mAdapterListShowPhotoDataShowProductBarcodeImage;
            private TextView mAdapterListShowPhotoDataClickProductDelText;
        }
    }

}
